package com.study.common.helper;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.study.common.core.domain.model.AppLoginUser;
import com.study.common.enums.UserType;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * 登录鉴权助手
 *
 * @author mjzx
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AppLoginHelper {

    public static final String APP_LOGIN_USER_KEY = "appLoginUser";
    public static final String APP_USER_KEY = "appUserId";

    /**
     * 登录系统
     *
     * @param appLoginUser App登录用户信息
     */
    public static void login(AppLoginUser appLoginUser) {
        SaStorage storage = SaHolder.getStorage();
        storage.set(APP_LOGIN_USER_KEY, appLoginUser);
        storage.set(APP_USER_KEY, appLoginUser.getUserId());
        SaLoginModel model = new SaLoginModel();
        StpUtil.login(appLoginUser.getLoginId(), model.setExtra(APP_USER_KEY, appLoginUser.getUserId()));
        StpUtil.getTokenSession().set(APP_LOGIN_USER_KEY, appLoginUser);
    }

    /**
     * 获取用户(多级缓存)
     */
    public static AppLoginUser getLoginUser() {
        AppLoginUser appLoginUser = (AppLoginUser) SaHolder.getStorage().get(APP_LOGIN_USER_KEY);
        if (appLoginUser != null) {
            return appLoginUser;
        }
        appLoginUser = (AppLoginUser) StpUtil.getTokenSession().get(APP_LOGIN_USER_KEY);
        SaHolder.getStorage().set(APP_LOGIN_USER_KEY, appLoginUser);
        return appLoginUser;
    }

    /**
     * 获取用户基于token
     */
    public static AppLoginUser getLoginUser(String token) {
        return (AppLoginUser) StpUtil.getTokenSessionByToken(token).get(APP_LOGIN_USER_KEY);
    }

    /**
     * 获取用户id
     */
    public static Long getUserId() {
        Long userId;
        try {
            userId = Convert.toLong(SaHolder.getStorage().get(APP_USER_KEY));
            if (ObjectUtil.isNull(userId)) {
                userId = Convert.toLong(StpUtil.getExtra(APP_USER_KEY));
                SaHolder.getStorage().set(APP_USER_KEY, userId);
            }
        } catch (Exception e) {
            return null;
        }
        return userId;
    }

    /**
     * 获取用户手机号码
     */
    public static String getUserPhone() {
        return getLoginUser().getUserPhone();
    }

    /**
     * 获取用户手机号码区号
     */
    public static String getAreaCode() {
        return getLoginUser().getAreaCode();
    }

    /**
     * 获取用户Email
     */
    public static String getUserEmail() {
        return getLoginUser().getUserEmail();
    }

    /**
     * 获取用户编码
     */
    public static String getUserCode() {
        return getLoginUser().getUserCode();
    }

    /**
     * 获取用户类型
     */
    public static UserType getUserType() {
        String loginId = StpUtil.getLoginIdAsString();
        return UserType.getUserType(loginId);
    }

}
